package rak.r91.controller;

import java.awt.Image;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import rak.r91.data.Item;
import rak.r91.data.ItemAction;
import rak.r91.data.ItemStore;
import rak.r91.data.Store;
import rak.r91.data.User;
import rak.r91.service.SimpleStoreManager;
import rak.r91.service.StoreManager;

@Controller
@RequestMapping("/store")
@SessionAttributes({"user" ,"item"})
public class StoreController {
	StoreManager sm = new SimpleStoreManager();
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView storePage(@ModelAttribute("item")Item item){
		ModelAndView mav = new ModelAndView("store");
		mav.addObject("itemStoreList", sm.getStores(item));
		return mav;
	}
	
	@RequestMapping("/register")
	public ModelAndView registerPage(@ModelAttribute("item")Item item){
		ModelAndView mav = new ModelAndView("register");
		mav.addObject("storeList", sm.getStores());
		return mav;
	}
	
	@RequestMapping("/register1")
	public ModelAndView register(@ModelAttribute("item")Item item,@ModelAttribute("user")User user, HttpServletRequest request){
		ItemAction itemAction = new ItemAction();
		Store store = new Store();
		item.setStorePrice(Double.parseDouble(request.getParameter("store_price")));
		store.setStoreID(Integer.parseInt(request.getParameter("store")));
		itemAction.setEmployee(user);
		itemAction.setStoreTo(store);
		itemAction.setItemCount(Integer.parseInt(request.getParameter("count")));
		itemAction.setActionNote(request.getParameter("note"));
		itemAction.setItem(item);
		sm.register(itemAction);
		ModelAndView mav = new ModelAndView("register");
		mav.addObject("storeList", sm.getStores());
		return mav;
	}
	
	@RequestMapping("/unregister")
	public ModelAndView unregisterPage(@ModelAttribute("item")Item item){
		ModelAndView mav = new ModelAndView("unregister");
		mav.addObject("itemStoreList", sm.getStores(item));
		//mav.addObject("storeList", sm.getStores());
		return mav;
	}
	
	@RequestMapping("/unregister1")
	public ModelAndView unregister(HttpServletRequest request, @ModelAttribute("item")Item item, @ModelAttribute("itemStoreList")List<ItemStore> itemStoreList){
		ModelAndView mav = new ModelAndView("unregister");
		ItemAction itemAction = new ItemAction();
		ItemStore itemStore = null;
		for (ItemStore is : itemStoreList) {
			if(Integer.parseInt(request.getParameter("store"))==is.getItemStoreID()){
				itemStore = is;
			}
		}
		itemAction.setItem(item);
		itemAction.setItemCount(Integer.parseInt(request.getParameter("count")));
		itemAction.setActionNote(request.getParameter("count"));
		sm.unregister(itemAction, itemStore);
		return mav;
	}
	
	
	
}
